package com.ruanjian.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruanjian.reggie.common.R;
import com.ruanjian.reggie.dto.DishDto;
import com.ruanjian.reggie.entity.Category;
import com.ruanjian.reggie.entity.Dish;
import com.ruanjian.reggie.entity.DishFlavor;
import com.ruanjian.reggie.service.CategoryService;
import com.ruanjian.reggie.service.DishFlavorService;
import com.ruanjian.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/***
 * 菜品管理
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 添加新菜品功能
     * @param dishDto 继承了dish类
     * @return
     */
    @PostMapping()
    public R<String> add(@RequestBody DishDto dishDto){
        dishService.saveDishAndDishFlavor(dishDto);
        return R.success("添加成功");

    }
    @GetMapping("/page")
    public R<IPage> list(int page,int pageSize,String name){
        /**
         * 这段代码是自己写的也可以用就是比较繁琐
         */
       /*
        Page page1= new Page(page,pageSize);
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        Page res=null;
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
             res = dishService.page(page1, queryWrapper);
             return R.success(res);
        }
        else {
            res = dishService.page(page1, null);

        }*/

        /**
         * 这是视频中的写法
         */
        Page<Dish> dishPage = new Page<>(page, pageSize);
        Page<DishDto> dishDtoPage = new Page<>();
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null, Dish::getName, name);
        wrapper.orderByDesc(Dish::getUpdateTime);
        dishService.page(dishPage, wrapper);//条件构造器中可以加泛型也可以不加
        //对象拷贝忽略records属性
        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");
        List<Dish> records = dishPage.getRecords();
        List<DishDto> collect = records.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if(category !=null){
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }
            return dishDto;


        }).collect(Collectors.toList());
        dishDtoPage.setRecords(collect);


        /**
         * 这是我自己写的方法
         */
//        IPage<List<Map<String, Object>>> iPage = dishService.queryListPage(page, pageSize, name);
//        return R.success(iPage);

        return R.success(dishDtoPage);
    }

    /**
     * 简单的批量删除功能
     * @param ids
     * @return
     */
    @DeleteMapping()
    public R<String> deleteByBeach(String ids){
        List<String> collect = Arrays.stream(ids.split(",")).collect(Collectors.toList());
        dishService.removeByIds(collect);
        //菜品关联下面的口味
        dishFlavorService.remove(new LambdaQueryWrapper<DishFlavor>().in(DishFlavor::getDishId,collect));

        return R.success("批量删除成功");
    }

    /**
     * 菜品的起售和停售
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> putStates(@PathVariable("status") int status,String ids){
        List<String> collect = Arrays.stream(ids.split(",")).collect(Collectors.toList());
        if(status==1){
            dishService.update(new UpdateWrapper<Dish>().in("id",collect).set("status",1));
            return R.success("菜品已经起售");

        }else {
            dishService.update(new UpdateWrapper<Dish>().in("id",collect).set("status",0));
            return R.success("菜品已经停售");

        }
    }

    /**
     * 通过id回显菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> getOne(@PathVariable("id") String id){
        Dish byId = dishService.getById(id);
        DishDto dishDto = new DishDto();
        List<DishFlavor> dishFlavors = dishFlavorService.queryByDishId(id);
        BeanUtils.copyProperties(byId,dishDto);
        dishDto.setFlavors(dishFlavors);
        return R.success(dishDto);
    }
    @PutMapping
    public R<String> updateDish(@RequestBody DishDto dishDto){
        dishService.updateDishAndFlavor(dishDto);

        return R.success("修改成功");
    }

    /**
     * 通过categoryId来查询菜品列表
     * @return
     */
//    @GetMapping("/list")
//    public R<List<Dish>> getList(String categoryId){
//        List<Dish> dishList = dishService.getDishList(categoryId);
//
//        return R.success(dishList);
//    }
    @GetMapping("/list")
    public R<List<DishDto>> List(Dish dish){
        List<DishDto> dishDtoList = dishService.getDishDtoList(dish);
        return R.success(dishDtoList);
    }

}
